package thread;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * Created with IntelliJ IDEA.
 * Description: 生产者消费者模型
 * User: 王圆豪
 * Date: 2025-05-03
 * Time: 20:52
 */
public class Demo28 {
    public static void main(String[] args) {
        BlockingQueue<Integer> l = new LinkedBlockingQueue<>();
        Thread producer = new Thread(()->{
            int n = 0;
            while (true){
                System.out.println("生产元素：" + n);
                try {
                    l.put(n);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                n++;
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        Thread consumer = new Thread(()->{
            while (true) {
                try {
                    System.out.println("消费元素：" + l.take());
//                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });

        producer.start();
        consumer.start();
    }
}
